package ipt.dao.f3;

import ipt.enumeracion.Rol;
import ipt.exp.AlumnoException;
import ipt.vo.Alumno;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class AlumnoTXTDAO implements AlumnoDAO {

	File connection = null;
	PreparedStatement ptmt = null;
	ResultSet resultSet = null;


	public AlumnoTXTDAO() {

	}

	private File getConnection() throws SQLException {
		File conn;
		conn = ConnectionFactoryTXT.getInstance().getConnection();
		return conn;
	}

	public List<Alumno> getAlumnos () {
    	 
    	 	List<Alumno> lista = new ArrayList<Alumno>();
    	 	FileReader fr=null;
    	 	BufferedReader br=null;
    	 	try { 
    	 		connection= getConnection();
        	 	fr=new FileReader(connection);
        	 	br=new BufferedReader(fr);
    			String s;  
    			s = br.readLine(); 
    			while(s!=null) { 
    				StringTokenizer st= new StringTokenizer(s,","); 
    				Alumno al= new Alumno(); 
    				al.setNombre(st.nextToken()); 
    				al.setApellido1(st.nextToken()); 
    				al.setApellido2(st.nextToken()); 
    				al.setEdad(Integer.parseInt(st.nextToken())); 
    				al.setEmail(st.nextToken()); 
    				al.setDni(st.nextToken()); 
    				String rol=st.nextToken();
    				if (rol.equalsIgnoreCase("TRABAJADOR")){
    					al.setRol(Rol.TRABAJADOR);
    				} else if (rol.equalsIgnoreCase("JEFE")){
    					al.setRol(Rol.JEFE);
    				} else {
    					al.setRol(Rol.CLIENTE);
    				}
    				lista.add(al); 
    				s=br.readLine(); 
    			}
    		} catch(Exception e){
    			e.printStackTrace();
    		} finally {
    			try {
    				if (br!=null)br.close();
    				if (fr!=null)fr.close();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	 	
			return lista;
     }

	@Override
	public Alumno getAlumnoByDni(String dni) throws AlumnoException {
		
   	 	Alumno alu=null;
//	 	try{ 
//            String queryString = "SELECT * FROM "+TABLE_NAME+" WHERE "+COL_DNI+"='"+dni+"'";
//            connection = getConnection();
//            ptmt = connection.prepareStatement(queryString);
//                 resultSet = ptmt.executeQuery();
//                 while (resultSet.next()) {
//                	 alu=new Alumno();
//                	 alu.setDni( resultSet.getString( COL_DNI ) );
//                	 alu.setNombre( resultSet.getString( COL_NOMBRE ) );
//                	 alu.setDni( resultSet.getString( COL_APELLIDO1 ) );
//                	 alu.setDni( resultSet.getString( COL_APELLIDO2 ) );
//                	 alu.setEdad( resultSet.getInt( COL_EDAD ) );
//                	 int rol=resultSet.getInt( COL_ROL );
//                	 if (rol==0) alu.setRol(Rol.TRABAJADOR);
//                	 else if (rol==1) alu.setRol(Rol.JEFE);
//                	 else if (rol==2) alu.setRol(Rol.CLIENTE);
//                	 alu.setEmail( resultSet.getString( COL_EMAIL ) );
//                 }
//           
//	 	 } catch ( AlumnoException e){        
//	 		 	e.printStackTrace();
//         } catch (SQLException e) {
//                 e.printStackTrace();
//         } finally {    	 
//                 try {
//                         if (resultSet != null)
//                                 resultSet.close();
//                         if (ptmt != null)
//                                 ptmt.close();
//                         if (connection != null)
//                                 connection.close();
//                 } catch (SQLException e) {
//                         e.printStackTrace();
//                 } catch (Exception e) {
//                         e.printStackTrace();
//                 }       
//         }
		return alu;
	}

	@Override
	public boolean udapteAlumno(Alumno alu) throws AlumnoException {
		return false;
	}

	@Override
	public boolean deleteAlumno(String dni) throws AlumnoException {
		return false;
	}

	@Override
	public boolean insertAlumno(Alumno alu) throws AlumnoException {
		return false;
	}

}
